;
;
;      ---------========= DSx86 Configuration Editor =========---------
;                               by RyouArashi
;                        http://ryouarashi.blogspot.com
;
;
;              ---------========= License Agreement =========---------
;
;Parts of this File are Copyright 1998 Alex Russel.
;Visit http://www3.telus.net/alexander_russell/course/chapter_4.htm
;
;Copyright 2010 RyouArashi. All rights reserved.
;
;Redistribution and use in source and binary forms, with or without
;modification, are permitted provided that the following conditions are met:
;
;   1. Redistributions of source code must retain the above copyright notice,
;      this list of conditions and the following disclaimer.
;
;   2. Redistributions in binary form must reproduce the above copyright notice,
;      this list of conditions and the following disclaimer in the documentation
;      and/or other materials provided with the distribution.
;
;THIS SOFTWARE IS PROVIDED BY RyouArashi ``AS IS'' AND ANY EXPRESS OR IMPLIED
;WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
;EVENT SHALL RyouArashi OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
;INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
;ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

global _Int09Handler
global _Int09FIFO
global _Int09RD
global _Int09WR

segment _DATA byte public class=DATA
; FIFO for Scancodes
_Int09FIFO:
    dd 0, 0, 0, 0 ; 16 Bytes FIFO
_Int09RD:
    db 0          ; Read Pointer
_Int09WR:
    db 0          ; Write Pointer


segment _TEXT byte public class=CODE
; IMPORTANT: These functions assume "Small" memory model!

; Int 0x09 Handler
_Int09Handler:
    ; Disable Interupts
    cli

    ; Push all used Regs
    push ax
    push bx
    push dx
    push ds

    ; Access Data Segment
    mov  ax, _DATA
    mov  ds, ax

    ; Get Scancode
    in   al, 0x60     ; Read Scancode             -+
    push ax           ; Push for later use         | Credit for this part
    in   al, 0x61     ; Read Keyboard Status       | goes to Alex Russell,
    mov  bl, al       ; Backup to bl               | Copyright 1998
    or   al, 0x80     ; Set Bit 7                  |
    out  0x61, al     ; Write Bit 7 set            | See note above
    mov  al, bl       ; Restore                    |
    out  0x61, al     ; write Bit 7 Clear          |
    mov  al, 0x20     ; Command 0x20 on Port 0x20  |
    out  0x20, al     ; = Reset PIC               -+

    ; Write to FIFO
    pop  dx                ; Get Scan Code
    mov  bx, _Int09FIFO    ; Base Address
    xor  ax, ax            ; Clear AX
    mov  al, [_Int09WR]    ; Write Pointer
    add  bx, ax            ; Compute WR Address
    mov  [ds:bx], dl       ; Write Scan Code to FIFO
    inc  al                ; Advande WR Pointer
    and  al, 0x0F          ; Roll-Over at 16 Bytes
    mov [_Int09WR], al     ; Save Write Pointer

    ; Pop all Regs
    pop  ds
    pop  dx
    pop  bx
    pop  ax

    ; Enable Interupts
    sti

    ; Return from Interupt
    iretw

; End of File